# coding=utf-8
#
# 版权所有 2011 沙发客团队
# 遵循Apache License 2.0协议
#    具体见 http://www.apache.org/licenses/LICENSE-2.0
# 此模块主要是用于打包生成安装程序，需要在命令行模式下输入下面字符串：
#    python setup.py py2exe
#
from distutils.core import setup
import py2exe
import time
import sys, os
import win32com.client
import shutil, sitecustomize

# If run without args, build executables, in quiet mode.
if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")
    
def GetSvnRevision():
    """
    获取svn的版本号.
    """
    svnRevision = 0
    try:
        import pysvn
        sourceDir = "."
        client = pysvn.Client()
        svnRevision = 0
        for status in client.status(sourceDir, ignore=True):
            if status.is_versioned:
                if status.entry.revision.number > svnRevision:
                    svnRevision = status.entry.revision.number
    except:
        pass
    return svnRevision

# ModuleFinder can't handle runtime changes to __path__, but win32com uses them
class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the versioninfo resources
        self.version = u'0.1.%04d.0' % GetSvnRevision()
        self.company_name = u"沙发客团队"
        self.copyright = u"沙发客团队"
        self.name = u'shafake'

try:
    # if this doesn't work, try import modulefinder
    import py2exe.mf as modulefinder
    import win32com
    for p in win32com.__path__[1:]:
        modulefinder.AddPackagePath("win32com", p)
    for extra in ["win32com.shell"]: #,"win32com.mapi"
        __import__(extra)
        m = sys.modules[extra]
        for p in m.__path__[1:]:
            modulefinder.AddPackagePath(extra, p)
except ImportError:
    # no build path setup, no worries.
    pass

#
# 移动build和dist目录
#
shutil.rmtree("build", ignore_errors = True)
shutil.rmtree("dist", ignore_errors = True)


# 编码
#includes = ["encodings", "encodings.gb2312"]
includes = ["encodings", 
             "encodings.*",]

options = {"py2exe":  
            {   "compressed": 1,  
                "optimize": 1,  
                "includes": includes,  
                "bundle_files": 2,
                "ascii": False, 
            }
          }

icon_resources_ebox = [(0, "shafake.ico")]

data_files = []

windows_target = Target(
    # used for the versioninfo resource
    description = u"沙发客, 让沙发飞一会",
    icon_resources = icon_resources_ebox,
    # what to build
    script = "ui.py",
    
    dest_base = "shafake",  # 目标名称
    )

# 安装程序
setup(
    data_files = data_files,
    description = u"shafake",
    name = "shafake",
    options = options,  
    zipfile = None,  
    windows = [windows_target],
    )